import { E_EVENT_KEY, E_EVENT_HANDLE, ThinkEditorEvent, ThinkEditorEventData } from "./ThinkEditor.Defined";
///ThinkEditorsInstance对应Tabs
export class ThinkEditorInstance extends EventTarget {
    constructor(instanceId) {
        super();
        this.editors = new Array();
        this.instanceId = instanceId;
    }
    AddEditor(thinkEditor) {
        if (this.GetEditor(thinkEditor.docName) !== undefined) {
            console.error("无法添加相同docName的文档，请在外部UnInit该ThinkEditor！");
            return false;
        }
        if (this.editors.length == 0) {
            thinkEditor.selected = true;
        }
        this.editors.push(thinkEditor);
        this.addInnerEventListenerAgent(thinkEditor);
        return true;
    }
    SelectEditorByEditorId(editorId) {
        let editor = this.GetEditorByEditorId(editorId);
        if (editor === undefined) {
            return false;
        }
        return this.SelectEditor(editor.docName);
    }
    SelectEditor(docName) {
        if (this.GetEditor(docName) === undefined) {
            console.warn("SelectDoc失败：文档：", docName, " 在本实例中不存在！");
            return false;
        }
        let currentThinkEditor = this.GetSelectedEditor();
        if (currentThinkEditor != undefined) {
            currentThinkEditor.UnLoad();
        }
        let selectEditor;
        for (var i = 0; i < this.editors.length; i++) {
            if (this.editors[i].docName == docName) {
                this.editors[i].selected = true;
                selectEditor = this.editors[i];
            }
            else {
                this.editors[i].selected = false;
            }
        }
        //------------------------------
        if (selectEditor === undefined) {
            console.warn("SelectDoc失败：selectDocInfo is undefined");
            return false;
        }
        if (selectEditor === undefined) {
            console.warn("SelectDoc失败：thinkEditor is undefined");
            return false;
        }
        selectEditor.SelectDoc(docName);
        selectEditor.Load(selectEditor.container);
        selectEditor.selectedTime = new Date();
        this.CreateDocEditorFocusEvent(selectEditor);
        return true;
    }
    CreateDocEditorFocusEvent(thinkEditor) {
        this.CreateDocEditorEvent(E_EVENT_KEY.docEditorFocus, thinkEditor);
    }
    CreateDocEditorEvent(eventKey, thinkEditor) {
        let eventData = new ThinkEditorEventData(E_EVENT_HANDLE.event, eventKey, thinkEditor, thinkEditor.editorId);
        //主要用于 告知Manager，哪个编辑器 被选择了
        //eventData.editorId = thinkEditor.docName;
        thinkEditor.dispatchEvent(new ThinkEditorEvent(eventKey, eventData));
    }
    GetLastEditor() {
        let editor;
        for (var i = 0; i < this.editors.length; i++) {
            if (editor === undefined) {
                editor = this.editors[i];
            }
            if (this.editors[i].selectedTime > editor.selectedTime) {
                editor = this.editors[i];
            }
        }
        return editor;
    }
    CloseEditor(docName) {
        let closeEditor = this.GetEditor(docName);
        if (closeEditor === undefined) {
            console.log("需要关闭的文档不存在：" + docName);
            return false;
        }
        return this.CloseDocEditorHandle(closeEditor);
    }
    CloseAllEditors() {
        while (this.editors.length > 0) {
            this.CloseDocEditorHandle(this.editors[0]);
        }
    }
    CloseDocEditorHandle(closeThinkEditor) {
        this.removeInnerEventListenerAgent(closeThinkEditor);
        //this.removeEditorEventListener(closeDocInfo.thinkEditor);
        //closeDocInfo.Close();
        //let closeThinkEditor = closeDocInfo.thinkEditor;
        let docName = closeThinkEditor.docName;
        console.log("关闭文档：" + docName);
        if (closeThinkEditor !== undefined) {
            //不要关闭 1、使关闭速度更快 2、避免清理内存出现内存错误 closeThinkEditor.CloseDoc(docName);
            closeThinkEditor.UnInit();
        }
        this.editors.splice(this.editors.findIndex((x) => x.docName == docName), 1);
        //------------------------
        let lastEditor = this.GetLastEditor();
        if (lastEditor === undefined) {
            return false;
        }
        this.SelectEditor(lastEditor.docName);
        return true;
    }
    GetEditorByShowName(showName) {
        return this.editors.find((x) => x.showName == showName);
    }
    GetEditor(docName) {
        return this.editors.find((x) => x.docName == docName);
    }
    GetEditorByEditorId(editorId) {
        return this.editors.find((x) => (x === null || x === void 0 ? void 0 : x.editorId) == editorId);
    }
    GetSelectedEditor() {
        return this.editors.find((x) => x.selected);
    }
    GetSelectedDocName() {
        let selectedEditor = this.GetSelectedEditor();
        return selectedEditor ? selectedEditor.docName : "";
    }
    GetEditorCount() {
        return this.editors.length;
    }
    addInnerEventListenerAgent(editor) {
        //editor=》OnInnerEvent=》APP listener 内部产生的事件，被路由到this.OnInnerEvent
        /*Manager་监听c事件是有必要的
        例如 测试demo，用户先在其它tab->点击编辑器->EditTags mounted->此时需要更新EditTags.thinkEditor只能通过 this.$thinkEditorManager1.GetCurrentEditor()来更新
        加载后，用户可能再次点击其它editor,此时再利用EditTags.OnEditorFocus()事件来更新
        */
        for (let keyEvent in E_EVENT_KEY) {
            //console.log(keyEvent);
            editor.addEventListener(keyEvent, (e) => this.OnInnerEvent(e));
        }
    }
    removeInnerEventListenerAgent(editor) {
        //editor=》OnInnerEvent=》APP listener 内部产生的事件，被路由到this.OnInnerEvent
        for (let keyEvent in E_EVENT_KEY) {
            //console.log(keyEvent);
            editor.removeEventListener(keyEvent, (e) => this.OnInnerEvent(e));
        }
    }
    OnInnerEvent(evt) {
        let e = evt;
        console.log(new Date(), " OnInnerEvent==>", e);
        e.data.instanceId = this.instanceId;
        let ret = this.dispatchEvent(new ThinkEditorEvent(e.type, e.data));
        if (ret == false) {
            evt.preventDefault();
        }
        if (e.type == E_EVENT_KEY.editorFocus || e.type == E_EVENT_KEY.docEditorFocus) {
            this.OnEditorFocus(e);
        }
    }
    OnEditorFocus(evt) {
        let e = evt;
        let editorId = e.data.editorId;
        //console.log("Manager OnEditorFocus");
        this.SetFoucs(editorId);
    }
    SetFoucs(editorId) {
        if (editorId === undefined) {
            return false;
        }
        //this.SelectDocEditorByEditorId(editorId);
    }
}
